package view;

import java.util.ArrayList;
import java.util.TreeMap;
import javax.swing.table.AbstractTableModel;
import model.kwalificatiebeheer.Competentie;
import model.kwalificatiebeheer.CompetentieBestand;
import model.kwalificatiebeheer.Criterium;
import model.kwalificatiebeheer.Werkproces;

public class CompetentieTableModel extends AbstractTableModel{

	private Werkproces wp = null;
	private ArrayList<Competentie> cs;
	private String[] columnNames;
	
	public CompetentieTableModel(){
		cs = CompetentieBestand.getInstance().getCompetenties();
		String[] cNames = {"Competenties:"};
		columnNames = cNames;
	}	
	public CompetentieTableModel(Werkproces wp){
		this.wp = wp;
		String[] cNames = {"Competentie:", "Criteria:"};
		columnNames = cNames;
	}
	
	public String getColumnName(int col) {
		return columnNames[col];
	}
	
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		if(wp != null){		
			return wp.getCompetentieCriteria().size();
		}else{
			return cs.size();
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if(wp != null){	
			TreeMap<Competentie, Criterium> list = wp.getCompetentieCriteria();
			ArrayList<Competentie> comps = new ArrayList<Competentie>(list.keySet());
			Competentie comp = list.floorKey(comps.get(rowIndex));
			Criterium crit = list.get(comp);
			if(columnIndex == 0){
				return comp.getNaam();
			}else if(columnIndex == 1){
				String str = "Prestatie Indicator:\n"+crit.getPrestatieIndicator()+"\n\nComponenten:\n";
				for(String com : crit.getComponenten()){
					str += com+"\n";
				}
				return str;
			}
			return "";
		}else{
			if(columnIndex == 0){
				return cs.get(rowIndex).getId()+" - "+cs.get(rowIndex).getNaam();
			}
			return "";
		}
	}
	@Override
    public Class<?> getColumnClass(int columnIndex) {
       return String.class;
    }
}
